﻿/**
 * Copyright (C) 2011 original author and authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NGuice.Inject.Internal.Util;

namespace NGuice.Inject.Spi
{
    //complete!
    /// <summary>
    /// 给定一个类型的提供者lookup。显式的在模型中使用Binder.GetProvider(Type)指定：
    /// <code>
    /// Provider&lt;PaymentService&gt; paymentServiceProvider = GetProvider(typeof(PaymentService));
    /// </code>
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public sealed class ProviderLookup<T> : IElement
    {
        private readonly object source;
        private readonly Key<T> key;
        private IProvider<T> delegatee;

        public ProviderLookup(object source, Key<T> key)
        {
            this.source = Preconditions.CheckNotNull(source, "source");
            this.key = Preconditions.CheckNotNull(key, "key");
        }

        public object Source
        {
            get { return source; }
        }

        public Key<T> Key
        {
            get
            {
                return key;
            }
        }

        public T AcceptVisitor<T>(IElementVisitor<T> visitor)
        {
            return visitor.Visit(this);
        }

        /// <summary>
        /// </summary>
        /// <param name="delegatee"></param>
        public void InitializeDelegate(IProvider<T> delegatee)
        {
            Preconditions.CheckState(this.delegatee == null, "delegate already initialized");
            this.delegatee = Preconditions.CheckNotNull(delegatee, "delegate");
        }


        public void ApplyTo(IBinder binder)
        {
            InitializeDelegate(binder.WithSource(Source).GetProvider(key));
        }

        /// <summary>
        /// 返回委托的提供者，如果没有被初始化，则返回null。委托当元素被处理后初始化，否则将被用于创建一个注入器
        /// </summary>
        public IProvider<T> Delegatee
        {
            get
            {
                return delegatee;
            }
        }

        private class InternalProvider : IProvider<T>
        {
            private ProviderLookup<T> outer;

            public InternalProvider(ProviderLookup<T> outer)
            {
                this.outer = outer;
            }

            public T Get()
            {
                Preconditions.CheckState(outer.Delegatee != null, "This Provider cannot be used until the Injector has been created.");
                return outer.delegatee.Get();
            }
            public override string ToString()
            {
                return "Provider<" + typeof(T) + ">";
            }

            object IProvider.Get()
            {
                return Get();
            }
        }

        /// <summary>
        /// 返回查找的提供者。结果直到此lookup被初始化后才有效，他通常在注入器创建后发生。
        /// 如果你在之前使用，提供者将抛出IllegalStateException异常
        /// </summary>
        public IProvider<T> Provider
        {
            get
            {
                return new InternalProvider(this);
            }
        }
    }

}
